<script setup lang="ts">
import { getCategorySub, getCategoryList } from '@/api/categorySub'
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'

import GoodList from '@/views/Home/components/GoodsItems.vue'

const route = useRoute()

const categorySubData = ref<any>({}) // 接受面包屑导航数据

// 获取面包屑导航数据
const getCategorySubData = async () => {
  const res:any = await getCategorySub(route.params.id)
  categorySubData.value = res.result
  console.log(res);
}

onMounted(() => {
  getCategorySubData()
})

// 存储二级分类基础列表数据
const categoryListData = ref<any>([])
// 声明接受的参数
const listData = ref({
  categoryId:route.params.id,
  page:1,
  pageSize: 20,
  sortFiele: 'publishTime'
})
// 请求二级分类基础列表数据
const getCategoryListData = async () => {
  const res:any = await getCategoryList(listData.value)
  console.log(res.result);
  categoryListData.value = res.result.items
}
// 点击tab实现页面切换
const tabChang = () => {
  console.log(listData.value.sortFiele);
  listData.value.page = 1
  getCategoryListData()
}
onMounted(() => getCategoryListData())

// 定义无限滚动
const disabled = ref(false)
const load = async () => {
  // console.log('xxx');
  // 获取下一页数据
  listData.value.page++
  const res:any = await getCategoryList(listData.value)
  // ES6方法拼接列表新旧数据
  categoryListData.value = [...categoryListData.value,...res.result.items]
  // 加载完毕，停止监听，取消无限滚动
  if(res.result.items.length === 0) {
    console.log('stop'); 
    disabled.value = true
  }
}
</script>

<template>
  <div class="container ">
    <!-- 面包屑 -->
    <div class="bread-container">
      <el-breadcrumb separator=">">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: `/category/${categorySubData.parentId}` }">{{ categorySubData.parentName }}
        </el-breadcrumb-item>
        <el-breadcrumb-item>{{ categorySubData.name }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="sub-container">
      <el-tabs v-model="listData.sortFiele" @tab-change="tabChang">
        <el-tab-pane label="最新商品" name="publishTime"></el-tab-pane>
        <el-tab-pane label="最高人气" name="orderNum"></el-tab-pane>
        <el-tab-pane label="评论最多" name="evaluateNum"></el-tab-pane>
      </el-tabs>
      <div class="body" v-infinite-scroll="load" :infinite-scroll-disabled="disabled">
        <!-- 商品列表-->
        <GoodList v-for="goods in categoryListData" :goods="goods" :key="goods.id"></GoodList>
      </div>
    </div>
  </div>

</template>



<style lang="scss" scoped>
.bread-container {
  padding: 25px 0;
  color: #666;
}

.sub-container {
  padding: 20px 10px;
  background-color: #fff;

  .body {
    display: flex;
    flex-wrap: wrap;
    padding: 0 10px;
  }

  .goods-item {
    display: block;
    width: 220px;
    margin-right: 20px;
    padding: 20px 30px;
    text-align: center;

    img {
      width: 160px;
      height: 160px;
    }

    p {
      padding-top: 10px;
    }

    .name {
      font-size: 16px;
    }

    .desc {
      color: #999;
      height: 29px;
    }

    .price {
      color: $priceColor;
      font-size: 20px;
    }
  }

  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: center;
  }


}

// 商品列表样式
.goods-item {
  display: block;
  width: 220px;
  padding: 20px 30px;
  text-align: center;
  transition: all .5s;

  &:hover {
    transform: translate3d(0, -3px, 0);
    box-shadow: 0 3px 8px rgb(0 0 0 / 20%);
  }

  img {
    width: 160px;
    height: 160px;
  }

  p {
    padding-top: 10px;
  }

  .name {
    font-size: 16px;
  }

  .desc {
    color: #999;
    height: 29px;
  }

  .price {
    color: $priceColor;
    font-size: 20px;
  }
}
</style> 
